/* 
 * Distributed as part of jdiff
 * 
 * Copyright (C) 2008 QArks.com
 *
 * Author: Pierre Meyer <support@qarks.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1, as 
 * published by the Free Software Foundation.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; see the file LICENSE.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */
package diff.tela;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

import diff.misc.FileDiffResult;
import diff.misc.FileLine;
import diff.misc.ParsedFile;

public class DiffPanel extends JPanel {  

	private JList leftFile;
	private DefaultListModel leftFileModel;
	private JList rightFile;
	private DefaultListModel rightFileModel;

	private FileOverview leftFileOverview;
	private FileOverview rightFileOverview;

	private JScrollPane scroll1, scroll2;
	private boolean scrolling = false;
	private LineCellRenderer leftRenderer,rightRenderer;
	private TitledBorder leftTitleBorder,rightTitleBorder;
	private JPanel diffPanel, leftPanel, rightPanel;

	private String LeftString; 
	private String RightString;
	private String LeftLabel; 
	private String RightLabel;
	
	public DiffPanel() {

		setLayout(new BorderLayout(5,5));
		leftFileModel = new DefaultListModel();
		rightFileModel = new DefaultListModel();
		leftFile = new JList(leftFileModel);
		leftRenderer = new LineCellRenderer(false);
		rightRenderer = new LineCellRenderer(true);
		leftFile.setCellRenderer(leftRenderer);
		rightFile = new JList(rightFileModel);
		rightFile.setCellRenderer(rightRenderer);
		scroll1 = new JScrollPane(leftFile);
		scroll2 = new JScrollPane(rightFile);

		leftFile.setDragEnabled(true);
		rightFile.setDragEnabled(true);

		leftFile.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		rightFile.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

		leftFileOverview = new FileOverview(false);
		rightFileOverview = new FileOverview(true);

		leftPanel = new JPanel(new BorderLayout(5, 5));
		leftPanel.add(scroll1, BorderLayout.CENTER);
		leftPanel.add(leftFileOverview, BorderLayout.EAST);
		leftTitleBorder = new TitledBorder("-");
		leftPanel.setBorder(leftTitleBorder);
		rightPanel = new JPanel(new BorderLayout(5, 5));
		rightPanel.add(scroll2, BorderLayout.CENTER);
		rightPanel.add(rightFileOverview, BorderLayout.EAST);
		rightTitleBorder = new TitledBorder("-");
		rightPanel.setBorder(rightTitleBorder);
		
		leftFile.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent e) {
				rightFile.getSelectionModel().clearSelection();
			}
		});
		rightFile.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent e) {
				leftFile.getSelectionModel().clearSelection();
			}
		});
		diffPanel = new JPanel(new GridLayout(1,2,5,5));
		diffPanel.add(leftPanel, 0);
		diffPanel.add(rightPanel, 1);
		add(diffPanel,BorderLayout.CENTER);

		scroll1.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
			public void adjustmentValueChanged(AdjustmentEvent e) {
				if (!scrolling) {
					scrolling = true;
					scroll2.getVerticalScrollBar().setValue(scroll1.getVerticalScrollBar().getValue());
					scrolling = false;
				}
			}
		});
		scroll2.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
			public void adjustmentValueChanged(AdjustmentEvent e) {
				if (!scrolling) {
					scrolling = true;
					scroll1.getVerticalScrollBar().setValue(scroll2.getVerticalScrollBar().getValue());
					scrolling = false;
				}
			}
		});
		scroll1.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
			public void adjustmentValueChanged(AdjustmentEvent e) {
				if (!scrolling) {
					scrolling = true;
					scroll2.getHorizontalScrollBar().setValue(scroll1.getHorizontalScrollBar().getValue());
					scrolling = false;
				}
			}
		});
		scroll2.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
			public void adjustmentValueChanged(AdjustmentEvent e) {
				if (!scrolling) {
					scrolling = true;
					scroll1.getHorizontalScrollBar().setValue(scroll2.getHorizontalScrollBar().getValue());
					scrolling = false;
				}
			}
		});
	}

	public void onObjDropped() {
 	  
	  	String leftText = LeftString;
	  	String rightText = RightString;
	  	
	  	try{
  		
				leftFileModel.clear();
				rightFileModel.clear();
				Window window = SwingUtilities.getWindowAncestor(leftFile);
				
				leftTitleBorder.setTitle(LeftLabel);
				rightTitleBorder.setTitle(RightLabel);
				
				repaint();
				FileDiffMonitorDialog monitor;
				if (window instanceof Frame) {
					monitor = new FileDiffMonitorDialog( (Frame) window, leftText, rightText, this);
				}
				else {
					monitor = new FileDiffMonitorDialog( (Dialog) window, leftText, rightText, this);
				}
				monitor.start();
				
	  	}
		catch(Exception ex){
			Object options[] = new Object[] { "Ok"};
			JOptionPane.showOptionDialog(this,
					"An error occured while comparing version: "+ex.getMessage(),
					"Difference",
					JOptionPane.DEFAULT_OPTION,
					JOptionPane.ERROR_MESSAGE,
					null, options, options[0]);
		}
	}

	public void addNotify() {
		super.addNotify();
		Font font = getFont();
		int fontHeight = getFontMetrics(font).getHeight();
		leftFile.setFixedCellHeight(fontHeight);
		rightFile.setFixedCellHeight(fontHeight);
		leftRenderer.setFont(font.deriveFont(Font.PLAIN));
		rightRenderer.setFont(font.deriveFont(Font.PLAIN));
	}

	public void setContent(FileDiffResult analyze) {
		leftFileModel.clear();
		rightFileModel.clear();

		ParsedFile leftFile = analyze.getLeftFile();
		ParsedFile rightFile = analyze.getRightFile();
		for (FileLine line:leftFile.getLines()) {
			leftFileModel.addElement(line);
		}
		for (FileLine line:rightFile.getLines()) {
			rightFileModel.addElement(line);
		}

		leftFileOverview.setParsedFile(leftFile);
		rightFileOverview.setParsedFile(rightFile);
		repaint();
	}
	

	public String getLeftString() {
		return LeftString;
	}

	public void setLeftString(String leftLabel, String leftString) {
		LeftString = leftString;
		LeftLabel = leftLabel;
	}

	public String getRightString() {
		return RightString;
	}

	public void setRightString(String rightLabel, String rightString) {
		RightString = rightString;
		RightLabel = rightLabel; 
	} 

}
